package com.mapeditor.core 
{
	import AS3Util.Tr;
	import com.mapeditor.math.Vector3;
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author SE
	 */
	public class CreateXML
	{
		private var elements:Array;
		
		private static var instance:CreateXML;  
		
		public static function getInstance():CreateXML
		{
			if (instance == null)  
				instance = new CreateXML();  				
			return instance;  
		}
		function CreateXML() 
		{
			
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		
		private function _onRefSelect(e:Event):void
        {
                trace('select');
        }//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
        private function _onRefComplete(e:Event):void
        {
                //Tr.ace('Complete');
        }//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		public function save(elements:Array):void
		{
			this.elements = elements;
			var xmlContent:XML = new XML();
			var date:Date = new Date();
			
			xmlContent=<XnaContent />;

			var xml:XML;
			xml = <Asset Type="SharedContent.Level"/>;
			xml.appendChild(<name>Loguetown</name>);
			xml.appendChild(<coordinate>{elements[0].x} 0 {elements[0].y}</coordinate>);
			xml.appendChild(_cratePlatformXML());
			xml.appendChild(_createTowersXML());
			xml.appendChild(_crateShipXML());
			xml.appendChild(_crateRoomXML());
			xml.appendChild(_cratePortalXML());
			xml.appendChild(_cratePowerupXML());
			xml.appendChild(_crateBuildsXML());
			
			xmlContent.appendChild(xml);
			
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
			ba.writeUTFBytes(xmlContent);
			
			var fr:FileReference = new FileReference();
			fr.addEventListener(Event.SELECT, _onRefSelect);
			fr.addEventListener(Event.COMPLETE, _onRefComplete);
			
			fr.save(ba, "output.xml");
			
			
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _convertX(item:Item):Number
		{
			return -((elements[0].x - item.x)*10);
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _convertY(item:Item):Number
		{
			return -(elements[0].y - item.y)*10;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _convertXPoint(x:Number):Number
		{
			return -((elements[0].x - x)*10);
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _convertYPoint(y:Number):Number
		{
			return -((elements[0].y - y)*10);
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _cratePlatformXML():XML
		{
			var xml:XML;
					
			xml = <platforms/>;
			var appendTag:XML = new XML();
	
			var i:Number;
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.BRIDGE:
						var rotation:Number = 0;
						if (elements[i].id == "BridgeHorizontal")
							rotation = 1.55;
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<file>model/cenario/bridge</file>
										<position>{_convertX(elements[i])} -100 {_convertY(elements[i])}</position>
										<rotation>{rotation}</rotation>
									</Item>;
						xml.appendChild(appendTag);
						break;
					case Item.PLATFORM:
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<file>model/cenario/grid</file>
										<position>{_convertX(elements[i])} -100 {_convertY(elements[i])}</position>
										<rotation>0.0</rotation>
									</Item>;
						xml.appendChild(appendTag);
						break;
				}
				
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _crateBuildsXML():XML
		{
			var xml:XML;
					
			xml = <builds/>;
			var appendTag:XML = new XML();
	
			var i:Number;
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.BUILD:
						var file:String = "";
						switch(elements[i].id)
						{
							case "HouseCorner":
								file = "model/cenario/Pack/houseCorner/houseCorner";
								break;
							case "HouseRamp":
								file = "model/cenario/Pack/houseRamp/houseRamp";
								break;
						}
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<file>{file}</file>
										<position>{_convertX(elements[i])} -100 {_convertY(elements[i])}</position>
										<rotation>0.0</rotation>
									</Item>;
						xml.appendChild(appendTag);
					break;
				}
				
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		private function _crateShipXML():XML
		{
			var xml:XML;
					
			xml = <ships/>;
			var appendTag:XML = new XML();
	
			var i:Number;
			var p:String = "";
			
			var rotation:Number;
			var scale:Number;
			var scaleCollision:Number;
			var file:String;
			var pos_z:int = 0;
			
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.SHIP:
						
						switch(elements[i].id)
						{
							case "Ship01":
								rotation = 0;
								scale = 1;
								scaleCollision = 5;
								file = "ship02";
								break;
							case "Ship02":
								rotation = 3.0;
								scale = 120;
								scaleCollision = 0.1;
								file = "ship03";
								break;
							case "Ship03":
								rotation = 3.0;
								scale = 0.3;
								scaleCollision = 1;
								file = "ship04";
								break;
							case "Tank01":
								rotation = 1.6;
								scale = 1.3;
								scaleCollision = 1.5;
								file = "model_triax_wheels";
								pos_z = -100;
								break;
							case "Tank02":
								rotation = 1.6;
								scale = 1.3;
								scaleCollision = 1.5;
								file = "model_triax_hoover";
								pos_z = -100;
								break;
							case "Tank03":
								rotation = 1.6;
								scale = 1.3;
								scaleCollision = 1.5;
								file = "model_triax_tracks";
								pos_z = -100;
								break;
						}
						p = "";
						
						for each(var point:Vector3 in elements[i].points)
							p += "\n\t\t"+_convertXPoint(point.x)+" "+pos_z+" "+_convertYPoint(point.y);
						
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<position>{_convertX(elements[i])} {pos_z} {_convertY(elements[i])}</position>
										<life>{elements[i].life}</life>
										<speed>{elements[i].speed}</speed>
										<rotation>{rotation}</rotation>
										<scale>{scale}</scale>
										<scaleCollision>{scaleCollision}</scaleCollision>
										<tiroFrequence>{elements[i].frequence}</tiroFrequence>
										<distTargetAtack>{elements[i].dist}</distTargetAtack>
										<file>model/enemy/{file}</file>
										<fileTiro>model/tiro/tiro01</fileTiro>
										<fileSoundTiro>enemy_shot</fileSoundTiro>
										<fileSoundCollision>enemy_collide</fileSoundCollision>
										<fileSoundDead>explosion</fileSoundDead>
										<points>
											{p}
										</points>
									</Item>
									
						xml.appendChild(appendTag);
						break;
				}
				
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		private function _crateRoomXML():XML
		{
			var xml:XML;
					
			xml = <rooms/>;
			var appendTag:XML = new XML();
			
			var i:Number;
			for (i = 1; i < elements.length; i++)
			{
				if (elements[i].type == Item.ROOM)
				{
					appendTag = <Item>										
									<posMin>{_convertXPoint(elements[i].x)} 0 {_convertYPoint(elements[i].y)}</posMin>
									<posMax>{_convertXPoint(elements[i].x+elements[i].width-1)} 0 {_convertYPoint(elements[i].y+elements[i].height-1)}</posMax>
								</Item>;
					xml.appendChild(appendTag);
				}
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		private function _cratePortalXML():XML
		{
			var xml:XML;
					
			xml = <portals/>;
			var appendTag:XML = new XML();
	
			var i:Number;
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.PORTAL:
						appendTag = <Item>
										<destiny>Eisenwald</destiny>
										<position>{_convertX(elements[i])} -20 {_convertY(elements[i])}</position>
										<rotation>1.5</rotation>
										<scale>10.0</scale>
										<scaleCollision>20.0</scaleCollision>
										<file>model/portal/portal</file>
										<fileSoundCollision>portal</fileSoundCollision>
									</Item>;
						xml.appendChild(appendTag);
						break;
				}
				
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		private function _cratePowerupXML():XML
		{
			var xml:XML;
					
			xml = <powerups/>;
			var appendTag:XML = new XML();
	
			var i:uint;
			var scale:Number;
			var rotation:Number;
			var scaleCollision:Number;
			var file:String;
			var fileSound:String;
			
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.POWERUP:
						if (elements[i].id == "Life")
						{
							scale = 10;
							scaleCollision = 20;
							rotation = 0;
							file = "Health";
							fileSound = "powerup_life";
						}
						else
						{
							scale = 10;
							scaleCollision = 20;
							rotation = 0;
							file = "Bomb";
							fileSound = "powerup_life";
						}
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<life>{elements[i].powerup_life}</life>
										<power>{elements[i].powerup_tiro}</power>
										<position>{_convertX(elements[i])} -20 {_convertY(elements[i])}</position>
										<rotation>{rotation}</rotation>
										<scale>{scale}</scale>
										<scaleCollision>{scaleCollision}</scaleCollision>
										<file>model/powerup/{file}</file>
										<fileSoundCollision>{fileSound}</fileSoundCollision>
									</Item>;
						xml.appendChild(appendTag);
						break;
				}
			}
			return xml;
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
		
		private function _createTowersXML():XML
		{
			
			var xml:XML;
					
			xml = <towers/>;
			var appendTag:XML = new XML();
	
			var i:uint;
			var rotation:Number;
			var scale:Number;
			var scaleCollision:Number;
			var file:String;
			var distTiroPosStart:String;
			var pos_z:int = 0;
			
			for (i = 1; i < elements.length; i++)
			{
				switch(elements[i].type)
				{
					case Item.TOWER:
						if (elements[i].id == "Tower01")
						{
							scale = 0.1;
							scaleCollision = 0.4;
							rotation = 1.5;
							file = "tower01";
							distTiroPosStart = "0 0 0";
						}
						else if(elements[i].id=="Tower02")
						{
							scale = 30;
							scaleCollision = 3;
							rotation = 0;
							file = "launcher_head";
							distTiroPosStart = "320 -30 320";
							pos_z = -50;
						}
						else 
						{
							scale = 3;
							scaleCollision = 3;
							rotation = 1.6;
							file = "model_weapon1";
							distTiroPosStart = "200 100 200";
							pos_z = -100;
						}
						appendTag = <Item>
										<name>{elements[i].id}</name>
										<position>{_convertX(elements[i])} {pos_z} {_convertY(elements[i])}</position>
										<life>{elements[i].life}</life>
										<rotation>{rotation}</rotation>
										<scale>{scale}</scale>
										<scaleCollision>{scaleCollision}</scaleCollision>
										<tiroFrequence>{elements[i].frequence}</tiroFrequence>
										<distTargetAtack>{elements[i].dist}</distTargetAtack>
										<distTiroPosStart>{distTiroPosStart}</distTiroPosStart>
										<file>model/enemy/{file}</file>
										<fileTiro>model/tiro/tiro01</fileTiro>
										<fileSoundTiro>enemy_shot</fileSoundTiro>
										<fileSoundCollision>enemy_collide</fileSoundCollision>
										<fileSoundDead>explosion</fileSoundDead>
									</Item>;
						xml.appendChild(appendTag);
						break;
				}
			}
			
			return xml;
			
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
	}

}